package com.zengqingfa.exercise.jvm;

import java.io.FileInputStream;
import java.lang.reflect.Method;

/**
 * @author zengqingfa
 * @className MyClassLoaderTest
 * @description
 * @create 2021/10/23 14:35
 */
public class MyClassLoaderTest3 {

    static class MyClassLoader extends ClassLoader {

        private String classPath;

        public MyClassLoader(String classPath) {
            this.classPath = classPath;
        }

        private byte[] loadByte(String name) throws Exception {
            name = name.replaceAll("\\.", "/");
            FileInputStream fis = new FileInputStream(classPath + "/" + name + ".class");
            int len = fis.available();
            byte[] data = new byte[len];
            fis.read(data);
            fis.close();
            return data;
        }

        protected Class<?> findClass(String name) throws ClassNotFoundException {
            try {
                byte[] data = loadByte(name);
                //defineClass将一个字节数组转为Class对象，这个字节数组是class文件读取后最终的字节 数组。
                return defineClass(name, data, 0, data.length);
            } catch (Exception e) {
                e.printStackTrace();
                throw new ClassNotFoundException();
            }
        }

        protected Class<?> loadClass(String name, boolean resolve)
                throws ClassNotFoundException {
            synchronized (getClassLoadingLock(name)) {
                // First, check if the class has already been loaded
                Class<?> c = findLoadedClass(name);
                if (c == null) {
                    long t0 = System.nanoTime();

                    if (!name.startsWith("com.zengqingfa.exercise")) {
                        c = this.getParent().loadClass(name);
                    } else {
                        c = findClass(name);
                    }
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    long t1 = System.nanoTime();
                    // this is the defining class loader; record the stats
                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
                if (resolve) {
                    resolveClass(c);
                }
                return c;
            }
        }
    }

    public static void main(String args[]) throws Exception {
        MyClassLoader classLoader1 = new MyClassLoader("F:/test1");
        Class clazz = classLoader1.loadClass("com.zengqingfa.exercise.jvm.User1");
        Object obj1 = clazz.newInstance();
        Method method1 = clazz.getDeclaredMethod("print", null);
        method1.invoke(obj1, null);
        System.out.println(clazz.getClassLoader().getClass().getName());
        System.out.println("========================");
        MyClassLoader classLoader2 = new MyClassLoader("F:/test2");
        Class clazz2 = classLoader2.loadClass("com.zengqingfa.exercise.jvm.User1");
        Object obj2 = clazz2.newInstance();
        Method method2 = clazz2.getDeclaredMethod("print", null);
        method2.invoke(obj2, null);
        System.out.println(clazz.getClassLoader().getClass().getName());
    }
}